package action.listener;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import engine.Edge;
import engine.Vertex;
import gui.View;

public class SelectListener implements MouseListener
{
   private int active = 1;
   private Edge etmp = null;
   private View view;
   private Vertex vtmp = null;
   private int x, y;

   public SelectListener(View view)
   {
      super();
      this.view = view;
   }

   public void ActiveVertex()
   {
      for (int i = 0; i < view.getVertices().size(); i++)
         if (view.getVertices().get(i).getStatus() == active)
            System.out.println(view.getVertices().get(i).getName());
   }

   @Override
   public void mouseClicked(MouseEvent e)
   {
      // TODO Auto-generated method stub
   }

   @Override
   public void mouseEntered(MouseEvent e)
   {
      // TODO Auto-generated method stub
      view.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
   }

   @Override
   public void mouseExited(MouseEvent e)
   {
      // TODO Auto-generated method stub

   }

   @Override
   public void mousePressed(MouseEvent e)
   {
      // TODO Auto-generated method stub
      x = e.getX();
      y = e.getY();
      int node = view.isVertex(x, y);
      int edge = view.isEdge(x, y);

      if (edge < 0 && node < 0)
         resetActiveEdgeVertex();
      else if (node != -1)
      {
         resetActiveEdgeVertex();
         vtmp = view.getVertices().get(node);
         view.setSelection(vtmp.xPos() - 15, vtmp.yPos() - 15, 30, 30);
         if (vtmp.getStatus() != active)
            vtmp.setStatus(active);
      }
      else if (edge != -1)
      {
         resetActiveEdgeVertex();
         etmp = view.getEdges().get(edge);
         etmp.setStatus(active);
         view.setSelection(etmp.xPos() - 10, etmp.yPos() - 10, 28, 18);
      }

      // first it mark as a select edge then if edge already select it will
      // join
      view.repaint();

   }

   @Override
   public void mouseReleased(MouseEvent e)
   {
      // TODO Auto-generated method stub

   }

   public void resetActiveEdgeVertex()
   {
      view.resetSelection();
      vtmp = null;
      etmp = null;
   }

}
